$(function(){
	var path = window.location.href;
	var id = path.substring(path.indexOf("=") + 1);
	var token=localStorage.getItem("key");
	
	var goodsname=localStorage.getItem("goodsname");
	var specification=localStorage.getItem("goodsgg");
	var goodsnum=localStorage.getItem("goodsnum");
	var goodspostage=localStorage.getItem("goodspostage");
	var goodsprice=localStorage.getItem("goodsprice");
	console.log(goodspostage);
	$('.item-title').html(goodsname);
	$('.specification').html(specification);
	$('.text_box').html(goodsnum);
	$('.pay-logis').html(goodspostage);
	$('.J_Price').html(goodsprice);
	
	
	
	
	
	
	
	
	var item='<div class="bundle  bundle-last">'+
								'<div class="bundle-main">'+
									'<ul class="item-content clearfix">'+
										'<div class="pay-phone">'+
											'<li class="td td-item">'+
												'<div class="item-pic">'+
													'<a href="#" class="J_MakePoint">'+
														'<img src="../images/kouhong.jpg_80x80.jpg" class="itempic J_ItemImg"></a>'+
												'</div>'+
												'<div class="item-info">'+
													'<div class="item-basic-info">'+
														'<a href="#" class="item-title J_MakePoint" data-point="tbcart.8.11"></a>'+
													'</div>'+
												'</div>'+
											'</li>'+
											'<li class="td td-info">'+
												'<div class="item-props">'+
													'<span class="sku-line">规格：<span class="specification"></span></span>'+
												'</div>'+
											'</li>'+
											'<li class="td td-price">'+
												'<div class="item-price price-promo-promo">'+
													'<div class="price-content">'+
														'￥<em class="J_Price price-now">39.00</em>'+
													'</div>'+
												'</div>'+
											'</li>'+
										'</div>'+
										'<li class="td td-amount">'+
											'<div class="amount-wrapper ">'+
												'<div class="item-amount ">'+
													'<span class="phone-title">购买数量</span>'+
													'<div class="text_box">1</div>'+
												'</div>'+
											'</div>'+
										'</li>'+
										'<li class="td td-sum">'+
											'<div class="td-inner">'+
												'<em tabindex="0" class="J_ItemSum number">117.00</em>'+
											'</div>'+
										'</li>'+
										'<li class="td td-oplist">'+
											'<div class="td-inner">'+
												'<span class="phone-title">配送方式</span>'+
												'<div class="pay-logis">'+
													'快递<b class="sys_item_freprice">10</b>元'+
												'</div>'+
											'</div>'+
										'</li>'+

									'</ul>'+

								'</div>'+
							'</div>';
	
	
//	计算总价
	var pricez=parseInt($('.text_box').html())*parseInt($('.J_Price').html());
	$('.J_ItemSum').html("￥"+pricez)
	$('#J_ActualFee').html(pricez);
	$('.pay-sum').html(pricez);
	
	//调取默认地址
	$.ajax({
		type: "get",
		url: "/api/me/getDefaultAddress",
		async: true,
		data: {"token":token},
		dataType: "json",
		success:function(data){
			console.log(data.data);
			$("#user").html(data.data.recipient);
			$("#phone").html(data.data.phone);
			$('#provinces').html(data.data.province);
			$('#citys').html(data.data.city);
			$('#dists').html(data.data.district);
			$('#streets').html(data.data.address);
			addressId = data.data.addressId;
		}
	});
	//调取地址
	$.ajax({
		type:"get",
		url:"/api/me/getShippingAddress",
		async: true,
		data: {"token":token},
		dataType: "json",
		success:function(data){
			console.log(data);
			var list = data.data;
			if(list!='[]'&&list.length>0){
				for (var i=0;i<list.length;i++) {
			        str='<li class="user-addresslist '+(list[i].beDefault==0?'':'defaultAddr')+'">'+

							'<div class="address-left">'+
								'<div class="user DefaultAddr">'+

									'<span class="buy-address-detail">'+ 
               						'<span class="buy-user">'+list[i].recipient+' </span>'+
									'<span class="buy-phone">'+list[i].phone+'</span>'+
								'</div>'+
								'<div class="default-address DefaultAddr" id="address">'+
									'<span class="buy-line-title buy-line-title-type">收货地址：</span>'+
									'<span class="buy--address-detail" data-set="'+list[i].addressId+'"> '+
								   		'<span class="province">'+list[i].province+'</span> '+
										'<span class="city">'+list[i].city+'</span> '+
										'<span class="dist">'+list[i].district+'</span><br/> '+
										'<span class="street">'+list[i].address+'</span>'+
									'</span>'+
								'</div>'+
								'<ins class="deftip '+(list[i].beDefault==0?'hidden':'')+'">默认地址</ins>'+
							'</div>'+
							'<div class="clear"></div>'+

//							'<div class="new-addr-btn">'+
//								'<span class="new-addr-bar hidden">|</span>'+
//								'<a href="#">编辑</a>'+
//								'<span class="new-addr-bar">|</span>'+
//								'<a href="javascript:void(0);">删除</a>'+
//							'</div>'+

						'</li>';
					$(".addList").append(str);
				}
				
			}
			$(".user-addresslist").click(function() {
				$(this).addClass("defaultAddr").siblings().removeClass("defaultAddr");
				if($(this).hasClass("defaultAddr")){
					$("#user").html($(this).find(".buy-user").html());
					$("#phone").html($(this).find(".buy-phone").html());
					$('#provinces').html($(this).find(".province").html());
					$('#citys').html($(this).find(".city").html());
					$('#dists').html($(this).find(".dist").html());
					$('#streets').html($(this).find(".street").html());
					addressId = ($(this).find(".buy--address-detail").data('set'));
					console.log(addressId);
				}
			});
			$('#J_Go').on("click",function(){
				$.ajax({
					type:"post",
					url:"/api/me/submitOrder",
					async: true,
					data:JSON.stringify({"data": {
							"token": token,
							"addressId":addressId,
							"tradetype": 0,
							"goods": [{
								"goodsId": id,
								"num": goodsnum,
								"specification": specification
							}],
							"invoiceType": 0
						}}),
					dataType: "json",
					success:function(data){
						console.log(data);
//						window.open('../person/order.html');
					}
				});
			});
		}
	});
//	localStorage.removeItem("goodsname");
//	localStorage.removeItem("goodsgg");
//	localStorage.removeItem("goodsnum");
//	localStorage.removeItem("goodspostage");
//	localStorage.removeItem("goodsprice");
	
});